Date.prototype.yyyymmdd = function() {
    var yyyy = this.getFullYear().toString();
    var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
    var dd  = this.getDate().toString();
    return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding
};

App.register('Contributions', Contributions = {

    init: function() {
        this.setup();
    },
    
    setup: function() {
        var self = this;

        $("#filter_apply").click(function(e) {
            e.preventDefault();
            App.notify("contributions.history");
        });

        $("#clear_btn").click(function(e) {
            e.preventDefault();
            self.clear_filter();
            App.notify("contributions.history");
        });

        $("#history_content").delegate('ul.pagination li a','click',function(e){
            e.preventDefault();
            var href = $(this).attr('href');
            var page = href.slice(-1);//
            self.load_history(page);
        });

        App.listen('contributions.history', function() {
            self.load_history();
        });

        $("#filter_apply").click();


        $("#query_receipt_dt").mask("9999-99-99");

        self.bind_auto_complete($("#query_member_no"),$("#query_hidden_member_no"));

    },


    clear_filter:function(){
        $("#query_receipt_dt").val('');
        $("#query_receipt_no").val('');
        $("#query_member_no").val('');
        $("#query_hidden_member_no").val('');
        $("#query_contribution_type").val('0');
        $("#query_currency").val('0');
        $("#query_payment_type").val('0');
    },


    load_history:function(page){
        var self = this;

        $("#history_content").html("<div class='wrapper-lg text-center'><h4>Loading Profile. Please wait ...</h4></div>");

        if(typeof page == "undefined"){
            page = 1;
        }

        //member validation if any
        var t = $("#query_member_no").val();
        t = t.trim();
        //Make sure the hidden member no is cleared if the member no is cleared
        if(t.length == 0){
            $("#query_hidden_member_no").val('');
        }

        if(t.length > 0){
            var h = $("#query_hidden_member_no").val();
            h = h.trim();
            if(h.length <1){
                $("#history_content").html("<div class='wrapper-lg text-center'><h4>Please type member number and choose from the dropdown options </h4></div>");
                alertify.error('Member No Error');
                return
            }
        }

        var params = {
            'page': page,
            'query_receipt_dt': $("#query_receipt_dt").val(),
            'query_receipt_no': $("#query_receipt_no").val(),
            'query_member_no': $("#query_hidden_member_no").val(),
            'query_contribution_type': $("#query_contribution_type").val(),
            'query_currency': $("#query_currency").val(),
            'query_payment_type': $("#query_payment_type").val()
        };

        var url = $("#history_url").val() + "/paginate";

        $.ajax({
            url: url,
            data:params,
            dataType: 'json',
            type: 'post',
            success: function(res) {

                if(res.status == 'success'){
                    $("#history_content").html(res.render);
                    //self.renderImages(res);
                    //self.hide_loader();
                    //self.show_content();
                }
                else{
                    $("#history_content").html("<div class='wrapper-lg text-center'><h4>No Record Found</h4></div>");
                }
            },
            error: function(xhr) {
                alertify.error('Server Error');
                $("#history_content").html("<div class='wrapper-lg text-center'><h4>No Record Found</h4></div>");
                console.log(xhr.responseText);
            }
        });
    },


    bind_auto_complete:function(jquery_obj,jquery_hidden_obj){
        var self = this;

        jquery_obj.autocomplete({
            source: $("#contacts-autocomplete-url").val(),
            minLength: 2,
            select: function( event, ui ) {
                if ( ui.item ) {
                    //console.log(ui.item)
                    var hidden = jquery_hidden_obj;
                    hidden.val(ui.item.organization_member_id);
                }
            },
            create: function() {
                $(this).data('ui-autocomplete')._renderMenu = function( ul, items ) {
                    var that = this;

                    $.each(items, function( index, item ) {
                        var isPill = false;

                        $(".pillbox li").each(function() {
                            if( $(this).data('number') == item.number ) {
                                isPill = true;
                                return false;
                            }
                        });

                        if( !isPill ) {
                            that._renderItemData(ul, item);
                        }
                    });
                    $(ul).find("li:odd").addClass("odd");
                };

                $(this).data('ui-autocomplete')._renderItem = function( ul, item ) {
                    return $("<li>")
                        .append("<div class='name font-bold text-sm p-xs b-b bg-light'>" + item.label + "</div>").appendTo(ul);
                };

                $(this).data('ui-autocomplete')._resizeMenu = function() {
                    this.menu.element.outerWidth(300);
                };
            }
        });

    }
});


App.register("ContributionsEditor", ContributionsEditor = {

    init: function() {
        var self = this;

        $('#history_content').delegate('#collections_tbl_list tbody tr td i.edit-btn','click',function(){
            var parent_data_id = $(this).attr('parent-data-id');
            var tr = $("tr[data-contribution-id='" + parent_data_id + "']");
            self.process_row(tr);
        });

        $('#history_content').delegate('#collections_tbl_list tbody tr td i.delete-btn','click',function(){
            var parent_data_id = $(this).attr('parent-data-id');
            var tr = $("tr[data-contribution-id='" + parent_data_id + "']");
            self.delete_contribution(tr.attr('data-receipt-id'),tr.attr('data-contribution-id'));
        });

        $('#history_content').delegate('#collections_tbl_list tbody tr td i.cancel-btn','click',function(){
            var parent_data_id = $(this).attr('parent-data-id');
            var tr = $("tr[data-contribution-id='" + parent_data_id + "']");
            self.cancel_editing(tr);
        });

        $('#history_content').delegate('#collections_tbl_list tbody tr td i.save-btn','click',function(){
            var parent_data_id = $(this).attr('parent-data-id');
            var tr = $("tr[data-contribution-id='" + parent_data_id + "']");
            self.save_row(tr);
        });
    },


    delete_contribution:function(receipt_id,contribution_id){
        var self = this;
        alertify.confirm("Are you sure you want to delete this contribution record ?", function (e) {
            if (e) {
                self.show_row_loader(contribution_id,false);
                var url = $("#history_url").val()+'/delete';
                var params = {contribution_id: contribution_id};
                $.ajax({
                    url: url,
                    data: params,
                    type: 'post',
                    dataType: 'json',
                    success: function(response) {
                        self.show_row_loader(contribution_id,false,false);
                        if(response.status == 'success'){
                            $("tr[data-contribution-id='" + contribution_id + "']").hide('slow').remove();
                            alertify.success(response.message);
                        }
                        else{
                            alertify.error(response.message);
                        }
                    },
                    error: function(xhr) {
                        self.show_row_loader(contribution_id,false,false);
                        alertify.error("Failed to remove selected accounts. Please try again later");
                    }
                });
            } else {
                // user clicked "cancel"
            }
        });
    },



    show_row_loader:function(contribution_id,editing,state){
        if(typeof state == "undefined"){
            state = true;
        }
        if(state){
            $('#row-edit-btn_'+contribution_id).hide();
            $('#row-delete-btn_'+contribution_id).hide();
            $('#row-loader-btn_'+contribution_id).show();
            $('#row-save-btn_'+contribution_id).hide();
            $('#row-cancel-btn_'+contribution_id).hide();
        }
        else{
            $('#row-loader-btn_'+contribution_id).hide();
            if(editing){
                $('#row-save-btn_'+contribution_id).show();
                $('#row-cancel-btn_'+contribution_id).show();
            }
            else{
                $('#row-edit-btn_'+contribution_id).show();
                $('#row-delete-btn_'+contribution_id).show();
            }
        }
    },


    show_row_edit:function(contribution_id,state){
        if(typeof state == "undefined"){
            state = true;
        }
        if(state){
            $('#row-edit-btn_'+contribution_id).hide();
            $('#row-delete-btn_'+contribution_id).hide();
            $('#row-loader-btn_'+contribution_id).hide();
            $('#row-save-btn_'+contribution_id).show();
            $('#row-cancel-btn_'+contribution_id).show();
        }
        else{
            $('#row-save-btn_'+contribution_id).hide();
            $('#row-cancel-btn_'+contribution_id).hide();
            $('#row-loader-btn_'+contribution_id).hide();
            $('#row-edit-btn_'+contribution_id).show();
            $('#row-delete-btn_'+contribution_id).show();
        }
    },


    select_active_row:function(select_me){
        var self = this;

        $('#collections_tbl_list tbody tr.editing').each(function(){
            var me = $(this);
            if(me.hasClass('active') && (select_me.attr('data-id') != me.attr('data-id'))){//I was the last active row, validate and save me.
                self.process_row(me);
            }
            me.removeClass('active');
        });
        select_me.addClass('active');
    },


    process_row:function(tr){
        var self = this;
        //Check if there is any editing , then try save
        var tr_editing = $('#collections_tbl_list tbody tr.editing');
        if(tr_editing.length > 0){
            self.save_row(tr_editing,function(){
                self.add(tr);
            });
        }
        else{
            self.add(tr);
        }
    },


    add:function(tr){
        var self = this;

        var tr_id = tr.attr('data-contribution-id');

        tr.attr('class','editing').removeClass('saving').removeClass('row_error');

        //Receipt Date
        var td = tr.find("td:nth-child(1)");
        var value = td.attr("data-receipt_dt");
        var d = new Date(value);
        var value = d.yyyymmdd();
        var text = $('<input />').attr('type','text').attr('class','input-sm  input-s-m form-control col-md-1').attr('placeholder','').addClass('row_receipt_dt').attr('id','row_receipt_dt_'+tr_id);
        /*text.focus(function(){
            var parent_tr = text.parents().parents();
            self.select_active_row(parent_tr);
        });*/
        text.val(value).mask("9999-99-99");
        td.html('');
        td.append(text);


        //Receipt No
        var td = tr.find("td:nth-child(2)");
        var value = td.attr("data-receipt_no");
        var text = $('<input />').attr('type','text').attr('class','input-sm input-s-m form-control').attr('placeholder','Receipt No.').addClass('row_receipt_no').attr('id','row_receipt_no_'+tr_id);
        text.val(value);
        td.html('');
        td.append(text);


        //Contribution Type
        var td = tr.find("td:nth-child(3)");
        var value = td.attr("data-type");
        var select = $('<select />').attr('class','input-sm form-control input-s-sm inline').addClass('row_contributions_type').attr('id','row_contributions_type_'+tr_id);
        var default_val = '';
        for(var x in contribution_types){
            var opt_data = contribution_types[x];
            var option = $('<option />').attr('value',opt_data.id).html(opt_data.name);
            if(opt_data.name == value){
                default_val = opt_data.id;
            }
            select.append(option);
        }
        select.val(default_val);
        td.html('');
        td.append(select);


        //Member
        var td =tr.find("td:nth-child(4)");
        var value = td.attr("data-organisation_no");
        var value2 = td.attr("data-organisation_member_id");
        var text = $('<input />').attr('type','text').attr('class','input-sm  input-s-sm  form-control').attr('placeholder','Type Member No.').addClass('row_member_name').attr('id','row_member_name_'+tr_id);
        var hidden = $('<input />').attr('type','hidden').attr('value','').addClass('row_organisation_member_id').attr('id','row_organisation_member_id_'+tr_id);
        Contributions.bind_auto_complete(text,hidden);
        text.val(value);
        hidden.val(value2);
        td.html('');
        td.append(text);
        td.append(hidden);


        //Week Paying For
        var td = tr.find("td:nth-child(5)");
        var value = td.attr("data-week");
        var select = $('<select />').attr('class','input-sm form-control input-s-xs inline').addClass('row_week_paying').attr('id','row_week_paying_'+tr_id);
        var default_val = '';
        for(var x in weeks){
            var opt_data = weeks[x];
            var option = $('<option />').attr('value',x).html(opt_data);
            if(x == value){
                default_val = x;
            }
            select.append(option);
        }
        select.val(default_val);
        td.html('');
        td.append(select);


        //Month Paying For
        var td = tr.find("td:nth-child(6)");
        var value = td.attr("data-month");
        var select = $('<select />').attr('class','input-sm form-control input-s-xs inline').addClass('row_month_paying').attr('id','row_month_paying_'+tr_id);
        var default_val = '';
        for(var x in months){
            var opt_data = months[x].substring(0,3);
            var option = $('<option />').attr('value',x).html(opt_data);
            if(x == value){
                default_val = x;
            }
            select.append(option);
        }
        select.val(default_val);
        td.html('');
        td.append(select);


        //Year Paying For
        var td = tr.find("td:nth-child(7)");
        var value = td.attr("data-year");
        var select = $('<select />').attr('class','input-sm form-control input-s-xs inline').addClass('row_year_paying').attr('id','row_year_paying_'+tr_id);
        var default_val = '';
        for(var x in years){
            var opt_data = years[x];
            var option = $('<option />').attr('value',x).html(opt_data);
            if(opt_data == value){
                default_val = x;
            }
            select.append(option);
        }
        select.val(default_val);
        td.html('');
        td.append(select);


        //Payment Type
        var td = tr.find("td:nth-child(8)");
        var payment_type = td.attr("data-payment_type");
        var cheque_no = td.attr("data-cheque_no");

        var select = $('<select />').attr('class','input-sm form-control input-s-m pull-left inline').addClass('row_payment_type').attr('id','row_payment_type_'+tr_id);
        for(var value in payment_types){
            var value_id = payment_types[value]['id'];
            var opt_data = payment_types[value]['name'];
            var option = $('<option />').attr('value',value_id).html(opt_data);
            select.append(option);
        }
        select.change(function(){
            var val = $(this).val();
            if(val == 2){//Cheque then show cheque number text
                $('#row_cheque_no_'+tr_id).show('fast');
            }
            else{
                $('#row_cheque_no_'+tr_id).val('').hide('fast');
            }
        });
        select.val(payment_type);

        var text = $('<input />').attr('type','text').attr('class','input-sm input-s-m pull-left form-control m-r-xs row_cheque_no').attr('placeholder','Cheque No.').attr('id','row_cheque_no_'+tr_id);
        text.val(cheque_no);
        text.hide();
        td.html('');
        td.prepend(text);
        td.prepend(select);
        select.change();


        //Amount
        var td = tr.find("td:nth-child(9)");
        var value = td.attr("data-amount");
        var text = $('<input />').attr('type','text').attr('class','input-sm input-s-xs form-control m-r-xs row_amount').attr('placeholder','0.00').attr('id','row_amount_'+tr_id);
        text.val(value);
        td.html('');
        td.append(text);
        var select = $('<select />').attr('class','input-sm form-control input-s-xs pull-left inline').addClass('row_currency').attr('id','row_currency_'+tr_id);
        var default_val = default_currency;
        var data_currency = td.attr("data-currency");
        for(var x in currencies_list){
            var opt_data = currencies_list[x];
            var option = $('<option />').attr('value',x).html(opt_data);
            if(x == data_currency){
                default_val = x;
            }
            select.append(option);
        }
        select.val(default_val);
        td.prepend(select);

        //Save,Delete Button or Loader
        //var td = tr.find("td:nth-child(8)");
        self.show_row_edit(tr_id);

    },


    cancel_editing:function(tr){
        var self = this;

        var tr_id = tr.attr('data-contribution-id');

        tr.removeClass('editing').removeClass('saving').removeClass('row_error');

        //Receipt Date
        var td = tr.find("td:nth-child(1)");
        var value = td.attr("data-receipt_dt");
        td.html(value);

        //Receipt No
        var td = tr.find("td:nth-child(2)");
        var value = td.attr("data-receipt_no");
        td.html(value);

        //Contribution Type
        var td = tr.find("td:nth-child(3)");
        var value = td.attr("data-type");
        td.html(value);

        //Member
        var td =tr.find("td:nth-child(4)");
        var value = td.attr("data-organisation_no");
        td.html(value);

        //Week Paying For
        var td = tr.find("td:nth-child(5)");
        var value = td.attr("data-week");
        var default_val = '';
        for(var x in weeks){
            var opt_data = weeks[x];
            var option = $('<option />').attr('value',x).html(opt_data);
            if(x == value){
                default_val = opt_data;
            }
        }
        td.html(default_val);

        //Month Paying For
        var td = tr.find("td:nth-child(6)");
        var value = td.attr("data-month");
        var select = $('<select />').attr('class','input-sm form-control input-s-xs inline').addClass('row_month_paying').attr('id','row_month_paying_'+tr_id);
        var default_val = '';
        for(var x in months){
            var opt_data = months[x].substring(0,3);
            var option = $('<option />').attr('value',x).html(opt_data);
            if(x == value){
                default_val = opt_data;
            }
        }
        td.html(default_val);

        //Year Paying For
        var td = tr.find("td:nth-child(7)");
        var value = td.attr("data-year");
        var default_val = '';
        for(var x in years){
            var opt_data = years[x];
            var option = $('<option />').attr('value',x).html(opt_data);
            if(opt_data == value) {
                default_val = x;
            }
        }
        td.html(default_val);

        //Amount
        var td = tr.find("td:nth-child(8)");
        var value = td.attr("data-amount");
        var data_cur = td.attr("data-currency");
        console.log(data_cur);
        var currency = currencies_list[data_cur];
        var html = currency+' '+value;
        td.html(html);


        //Save,Delete Button or Loader
        //var td = tr.find("td:nth-child(8)");
        self.show_row_edit(tr_id,false);
    },



    animate_row_save:function(tr,editing,animate){
        var self = this;
        if(typeof animate == 'undefined'){
            animate = true;
        }

        var tr_id = tr.attr('data-contribution-id');
        var receipt_dt_obj = tr.find('td input#row_receipt_dt_'+tr_id);
        var receipt_no_obj = tr.find('td input#row_receipt_no_'+tr_id);
        var row_member_name_obj  = tr.find('td input#row_member_name_'+tr_id);
        var amount_obj  = tr.find('td input#row_amount_'+tr_id);
        var currency_obj  = tr.find('td select#row_currency_'+tr_id);
        var payment_type_obj  = tr.find('td select#row_payment_type_'+tr_id);
        var cheque_no_obj  = tr.find('td input#row_cheque_no_'+tr_id);
        var contributions_type_obj  = tr.find('td select#row_contributions_type_'+tr_id);
        var week_paying_obj  = tr.find('td select#row_week_paying_'+tr_id);
        var month_paying_obj  = tr.find('td select#row_month_paying_'+tr_id);
        var year_paying_obj  = tr.find('td select#row_year_paying_'+tr_id);

        if(animate){
            receipt_dt_obj.attr('disabled','disabled').removeClass('field_error');
            receipt_no_obj.attr('disabled','disabled').removeClass('field_error');
            row_member_name_obj.attr('disabled','disabled').removeClass('field_error');
            amount_obj.attr('disabled','disabled').removeClass('field_error');
            currency_obj.attr('disabled','disabled').removeClass('field_error');
            payment_type_obj.attr('disabled','disabled').removeClass('field_error');
            cheque_no_obj.attr('disabled','disabled').removeClass('field_error');
            contributions_type_obj.attr('disabled','disabled').removeClass('field_error');
            week_paying_obj.attr('disabled','disabled').removeClass('field_error');
            month_paying_obj.attr('disabled','disabled').removeClass('field_error');
            year_paying_obj.attr('disabled','disabled').removeClass('field_error');
            self.show_row_loader(tr_id,editing,true);
        }
        else{
            receipt_dt_obj.removeAttr('disabled').removeClass('field_error');
            receipt_no_obj.removeAttr('disabled').removeClass('field_error');
            row_member_name_obj.removeAttr('disabled').removeClass('field_error');
            amount_obj.removeAttr('disabled').removeClass('field_error');
            currency_obj.removeAttr('disabled').removeClass('field_error');
            payment_type_obj.removeAttr('disabled').removeClass('field_error');
            cheque_no_obj.removeAttr('disabled').removeClass('field_error');
            contributions_type_obj.removeAttr('disabled').removeClass('field_error');
            week_paying_obj.removeAttr('disabled').removeClass('field_error');
            month_paying_obj.removeAttr('disabled').removeClass('field_error');
            year_paying_obj.removeAttr('disabled').removeClass('field_error');
            self.show_row_loader(tr_id,editing,false);
        }

    },


    validate_row:function(tr){
        var self = this;

        var error_obj = [];
        var tr_id = tr.attr('data-contribution-id');

        var rrdt = tr.find('td input#row_receipt_dt_'+tr_id);
        var receipt_dt = rrdt.val();
        receipt_dt = receipt_dt.trim();
        if(receipt_dt.length <= 0){
            error_obj.push({obj:rrdt,msg:'Receipt Date is required.'})
        }

        var rrn = tr.find('td input#row_receipt_no_'+tr_id);
        var receipt_no = rrn.val();
        receipt_no = receipt_no.trim();
        if(receipt_no.length <= 0){
            error_obj.push({obj:rrn,msg:'Receipt No is required.'})
        }

        var romi = tr.find('td input#row_organisation_member_id_'+tr_id);
        var rmn = tr.find('td input#row_member_name_'+tr_id);
        rmn.removeClass('field_error');
        //if this is empty then the hidden field should be empty too
        var t_val = rmn.val();
        t_val = t_val.trim();
        if(t_val.length == 0){
            romi.val('');
        }
        var organisation_member_id  = romi.val();
        organisation_member_id = organisation_member_id.trim();
        if(organisation_member_id.length <= 0){
            error_obj.push({obj:rmn,msg:'Member No is required.'})
        }

        var ram = tr.find('td input#row_amount_'+tr_id);
        var amount  = ram.val();
        amount = amount.trim();
        if(amount.length <= 0){
            error_obj.push({obj:ram,msg:'Amount is required.'})
        }
        else{
            if(!jQuery.isNumeric(amount)){
                error_obj.push({obj:ram,msg:'Amount must be numeric.'})
            }
        }

        var pty = tr.find('td select#row_payment_type_'+tr_id);
        if(pty.val() == 2){//if Cheque, validate Cheque Number
            var cqn = tr.find('td input#row_cheque_no_'+tr_id);
            var cheque_no  = cqn.val();
            cheque_no = cheque_no.trim();
            if(cheque_no.length <= 0){
                error_obj.push({obj:cqn,msg:'Cheque Number is required.'})
            }
        }


        if(error_obj.length > 0){
            var txt = '';
            for(var x in error_obj){
                var t = error_obj[x];
                txt = txt + t.msg +' <br />';
                t.obj.addClass('field_error');
            }
            alertify.error(txt);
            return false;
        }
        else{
            return true;
        }
    },


    save_row:function(tr,callback){
        var self = this;

        //Validate
        var res = self.validate_row(tr);
        if(res){
        }
        else{ //Validation error, so back to editing
            tr.removeClass('saving').addClass('editing');
            return;
        }

        var tr_id = tr.attr('data-contribution-id');
        var receipt_dt = tr.find('td input#row_receipt_dt_'+tr_id).val();
        var receipt_no = tr.find('td input#row_receipt_no_'+tr_id).val();
        var organisation_member_id  = tr.find('td input#row_organisation_member_id_'+tr_id).val();
        var amount  = tr.find('td input#row_amount_'+tr_id).val();
        var currency  = tr.find('td select#row_currency_'+tr_id).val();
        var payment_type  = tr.find('td select#row_payment_type_'+tr_id).val();
        var cheque_no  = tr.find('td input#row_cheque_no_'+tr_id).val();
        var contributions_type  = tr.find('td select#row_contributions_type_'+tr_id).val();
        var week_paying  = tr.find('td select#row_week_paying_'+tr_id).val();
        var month_paying  = tr.find('td select#row_month_paying_'+tr_id).val();
        var year_paying  = tr.find('td select#row_year_paying_'+tr_id).val();

        //Animate Saving
        self.animate_row_save(tr,true);

        var query = [];
        var data = {
            'contribution_id':tr_id,
            'receipt_id':tr.attr('data-receipt-id'),
            'receipt_dt':receipt_dt,
            'receipt_no':receipt_no,
            'organisation_member_id':organisation_member_id,
            'amount':amount,
            'currency':currency,
            'payment_type':payment_type,
            'cheque_no':cheque_no,
            'contributions_type':contributions_type,
            'week_paying':week_paying,
            'month_paying':month_paying,
            'year_paying':year_paying
        };
        query.push(data);
        self.save(query,tr,callback);
    },

    save: function(data,tr,callback) {
        var self = this;

        var url = $("#history_url").val()+'/save';
        var params = 'data='+encodeURIComponent(JSON.stringify(data));

        $.ajax({
            url: url,
            data: params,
            type: 'post',
            dataType: 'json',
            success: function(res) {
                self.animate_row_save(tr,true,false);
                if( typeof res['status'] != "undefined" ) {
                    if( res['status'] == 'success' ) {
                        alertify.success(res['message']);
                        self.post_save(res.data);
                        if(typeof callback == "function"){
                            callback();
                        }
                    }
                    else {
                        alertify.error(res['message']);
                    }
                }
                else {
                    alertify.error(JSON.stringify(res));
                }
            },
            error: function() {
                self.animate_row_save(tr,true,false);
                alertify.error("Failed to save member contribution.");
            }
        });
    },


    post_save:function(data){
        var self = this;
        for(var x in data){
            var tr_id = data[x]['ModuleMemberContribution'][0]['id'];

            var tr = $("tr[data-contribution-id='" + tr_id + "']").removeClass();
            var err = data[x]['Error'];
            if(typeof err == 'undefined'){

                var receipt_dt_obj = tr.find('td input#row_receipt_dt_'+tr_id);
                var parent_td = receipt_dt_obj.parent('td');
                parent_td.attr("data-receipt_dt",receipt_dt_obj.val()).html(receipt_dt_obj.val());
                receipt_dt_obj.remove();

                var receipt_no_obj = tr.find('td input#row_receipt_no_'+tr_id);
                var parent_td = receipt_no_obj.parent('td');
                parent_td.attr("data-receipt_no",receipt_no_obj.val()).html(receipt_no_obj.val());
                receipt_no_obj.remove();

                var payment_type_obj  = tr.find('td select#row_payment_type_'+tr_id);
                var cheque_no_obj  = tr.find('td input#row_cheque_no_'+tr_id);
                var text  = payment_type_obj.find("option:selected").text();
                var parent_td = cheque_no_obj.parent('td');
                var html = text+'  &nbsp; ';
                var cheq_no = cheque_no_obj.val();
                cheq_no = cheq_no.trim();
                if(cheq_no.length > 0 ){
                    html += '( '+cheq_no+' ) Not Cleared';
                }
                parent_td.attr("data-payment_type",payment_type_obj.val()).attr("data-cheque_no",cheque_no_obj.val()).html(html);
                parent_td.html(html);
                payment_type_obj.remove();
                cheque_no_obj.remove();

                var amount_obj  = tr.find('td input#row_amount_'+tr_id);
                var currency_obj  = tr.find('td select#row_currency_'+tr_id);
                var parent_td = amount_obj.parent('td');
                var text  = currency_obj.find("option:selected").text();
                var html = text+' '+amount_obj.val();
                parent_td.attr("data-amount",amount_obj.val()).attr("data-currency",currency_obj.val()).html(html);
                amount_obj.remove();
                currency_obj.remove();


                var contributions_type_obj  = tr.find('td select#row_contributions_type_'+tr_id);
                var text  = contributions_type_obj.find("option:selected").text();
                var parent_td = contributions_type_obj.parent('td');
                parent_td.attr("data-type",text).html(text);
                contributions_type_obj.remove();

                var week_paying_obj  = tr.find('td select#row_week_paying_'+tr_id);
                var parent_td = week_paying_obj.parent('td');
                var text  = week_paying_obj.find("option:selected").text();
                var val  = week_paying_obj.find("option:selected").val();
                parent_td.attr("data-week",val).html(text);
                week_paying_obj.remove();

                var month_paying_obj  = tr.find('td select#row_month_paying_'+tr_id);
                var parent_td = month_paying_obj.parent('td');
                var text  = month_paying_obj.find("option:selected").text();
                var val  = month_paying_obj.find("option:selected").val();
                parent_td.attr("data-month",val).html(text);
                month_paying_obj.remove();

                var year_paying_obj  = tr.find('td select#row_year_paying_'+tr_id);
                var parent_td = year_paying_obj.parent('td');
                var text  = year_paying_obj.find("option:selected").text();
                var val  = year_paying_obj.find("option:selected").val();
                parent_td.attr("data-year",val).html(text);
                year_paying_obj.remove();

                var organisation_member_id = data[x]['ModuleMemberContribution'][0]['organisation_member_id'];
                var row_member_name_obj  = tr.find('td input#row_member_name_'+tr_id);
                var parent_td = row_member_name_obj.parent('td');
                parent_td.attr("data-organisation_member_id",organisation_member_id).attr("data-organisation_no",row_member_name_obj.val()).html(row_member_name_obj.val());
                row_member_name_obj.remove();


                self.show_row_edit(tr_id,false);
            }
            else{
                tr.removeClass('saving').addClass('editing').addClass('row_error');
                self.show_row_edit(tr_id,true);
                alertify.error(err);
            }

        }
    }
});
